package com.ruoyi.project.system.auth.controller;

import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.common.utils.text.Convert;
import com.ruoyi.framework.aspectj.lang.annotation.Log;
import com.ruoyi.framework.aspectj.lang.enums.BusinessType;
import com.ruoyi.framework.web.controller.BaseController;
import com.ruoyi.framework.web.domain.AjaxResult;
import com.ruoyi.framework.web.page.TableDataInfo;
import com.ruoyi.project.system.auth.domain.SysAuth;
import com.ruoyi.project.system.auth.service.AuthService;
import com.ruoyi.project.system.keystore.service.KeystoreService;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Objects;

/**
 * 认证信息
 *
 * @author ruoyi
 */
@Controller
@RequestMapping("/system/auth")
public class AuthController extends BaseController {
    private String prefix = "system/auth";

    @Autowired
    private AuthService authService;

    @Autowired
    private KeystoreService keystoreService;


    @RequiresPermissions("system:auth:view")
    @GetMapping()
    public String auth() {
        return prefix + "/auth";
    }

    @RequiresPermissions("system:auth:list")
    @PostMapping("/list")
    @ResponseBody
    public TableDataInfo list(SysAuth sysAuth) {
        startPage();
        List<SysAuth> list = authService.selectAuthList(sysAuth);
        return getDataTable(list);
    }


    /**
     * 修改认证
     */
    @RequiresPermissions("system:auth:edit")
    @GetMapping("/edit/{authId}")
    public String edit(@PathVariable("authId") Long authId, ModelMap mmap) {
        mmap.put("auth", authService.selectAuthById(authId));
        mmap.put("keystores", keystoreService.selectKeystoreAll());

        return prefix + "/edit";
    }

    /**
     * 修改保存认证
     */
    @RequiresPermissions("system:auth:edit")
    @Log(title = "认证管理", businessType = BusinessType.UPDATE)
    @PostMapping("/edit")
    @ResponseBody
    public AjaxResult editSave(@Validated SysAuth sysAuth) {
        return toAjax(authService.updateAuth(sysAuth));
    }

    @RequiresPermissions("system:auth:copy")
    @Log(title = "认证管理", businessType = BusinessType.INSERT)
    @PostMapping("/copy")
    @ResponseBody
    public AjaxResult copy(String ids) {
        return toAjax(authService.copyByIds(ids));
    }


    @RequiresPermissions("system:auth:remove")
    @Log(title = "认证管理", businessType = BusinessType.DELETE)
    @PostMapping("/remove")
    @ResponseBody
    public AjaxResult remove(String ids) {
        Long[] authIds = Convert.toLongArray(ids);
        if (authIds.length < 1) {
            return AjaxResult.warn("请选择需要删除的数据");
        }
        List<SysAuth> sysAuths = authService.selectByIds(authIds);
        if (sysAuths.stream().map(SysAuth::getInitial).anyMatch(initial -> Objects.equals(initial, 0))) {
            return AjaxResult.warn("存在默认认证源源，不允许删除");
        }

        return toAjax(authService.deleteAuthByIds(authIds));
    }

    /**
     * 认证状态修改
     */
    @Log(title = "认证管理", businessType = BusinessType.UPDATE)
    @RequiresPermissions("system:auth:edit")
    @PostMapping("/changeStatus")
    @ResponseBody
    public AjaxResult changeStatus(SysAuth sysAuth) {
        return toAjax(authService.changeStatus(sysAuth));
    }


    /**
     * 校验认证源名称
     */
    @PostMapping("/checkAuthNameUnique")
    @ResponseBody
    public boolean checkAuthNameUnique(SysAuth sysAuth) {
        return authService.checkAuthNameUnique(sysAuth);
    }


    /**
     * 校验client_name
     */
    @PostMapping("/checkClientNameUnique")
    @ResponseBody
    public boolean checkClientNameUnique(SysAuth sysAuth) {
        return authService.checkClientNameUnique(sysAuth);
    }

    /**
     * 校验元数据文件名
     */
    @PostMapping("/checkMetadataFilenameUnique")
    @ResponseBody
    public boolean checkMetadataFilenameUnique(SysAuth sysAuth) {
        if (StringUtils.isBlank(sysAuth.getMetadataFilename())) {
            return true;
        }
        return authService.checkMetadataFilenameUnique(sysAuth);
    }

}